package com.engine.jucailinkq.attendance.attendanceanalysis.service.impl;

import com.engine.jucailinkq.attendance.attendanceanalysis.cmd.item.AbsenteeismItemCmd;
import com.engine.jucailinkq.attendance.attendanceanalysis.cmd.item.ForGetClockItemCmd;
import com.engine.jucailinkq.attendance.attendanceanalysis.service.ForgetClockInService;
import com.engine.jucailinkq.attendance.enums.AccountingUnitEnum;
import com.engine.jucailinkq.attendance.enums.AttendanceItemTypeEnum;
import com.engine.jucailinkq.attendance.enums.CheckBoxEnum;
import com.engine.jucailinkq.attendance.enums.SystemItemEnum;
import com.engine.jucailinkq.common.util.DateUtil;
import com.engine.jucailinkq.common.util.Utils;
import com.engine.core.impl.Service;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class ForgetClockInServiceImpl extends Service implements ForgetClockInService {

    @Override
    public List<Map<String,Object>> forgetClockInAllDays(Map<String,Object> params) {
        Map<String,Object> forgetParam = Maps.newHashMap();
        forgetParam.put("item", SystemItemEnum.MISSING_CLOCK_IN.getKey());
        forgetParam.put("itemduration", 1);
        List<Map<String,Object>> resultList = Lists.newArrayList();
        resultList.add(forgetParam);
        return resultList;
    }

    @Override
    public List<Map<String,Object>> forgetClockIn(Map<String, Object> params) {
        Map<String, Object> classInfo = (Map<String, Object>)params.get("classInfo");

        //班段类型
        String bdlx = Util.null2String(classInfo.get("bdlx"));

        String workFor = Utils.getWorkFor(bdlx);
        params.put("workfor",workFor);
        params.put("key",SystemItemEnum.MISSING_CLOCK_IN.getKey());
        List<Map<String,Object>> attendanceItems = (List<Map<String,Object>>)params.get("attendanceItems");

        List<Map<String,Object>> resultList = Lists.newArrayList();
        attendanceItems = attendanceItems.stream().filter(e->e.get("key").equals(SystemItemEnum.MISSING_CLOCK_IN.getKey())).collect(Collectors.toList());
       if (attendanceItems.size() > 0){
           Map<String,Object> forgetParam = Maps.newHashMap();
           forgetParam.put("item", SystemItemEnum.MISSING_CLOCK_IN.getKey());
           forgetParam.put("itemduration", 1);
           forgetParam.put("itemType", AttendanceItemTypeEnum.MISSE_CARD);
           forgetParam.put("zysd",attendanceItems.get(0).get("zysd"));
           forgetParam.put("kczgsc", CheckBoxEnum.CHECKED.getKey());
           resultList.add(forgetParam);
       }
        return resultList;
    }

    @Override
    public List<Map<String, Object>> forgetConvertAbsenteeism(Map<String, Object> params) {
        List<Map<String,Object>> abnormalList = (List<Map<String,Object>>)params.get("abnormalList");
        List<Map<String, Object>> scheduleResult =  (List<Map<String, Object>>)params.get("scheduleResult");
        //考勤项目
        List<Map<String,Object>> attendanceItems = (List<Map<String,Object>>)params.get("attendanceItems");
        String analysisDate = Util.null2String(params.get("analysisDate"));
        List<Map<String,Object>> needRecordMissCardList = abnormalList.stream().filter(e-> e.get("itemType") == AttendanceItemTypeEnum.MISSE_CARD).collect(Collectors.toList());
        if (needRecordMissCardList.size() == 0){
            return abnormalList;
        }
        Map<String,Object> lateParam = Maps.newHashMap();
        lateParam.put("attendanceItems",attendanceItems);
        lateParam.put("forgetConvertAbsenteeism",CheckBoxEnum.CHECKED.getKey());
        List<Map<String,Object>> absenteeismList = Lists.newArrayList();
        for (Map<String, Object> scheduleMap:scheduleResult){
            String bdlx = scheduleMap.get("bdlx").toString();
            if (!Utils.ifRestClassSegment(bdlx) && !Utils.ifAskforOrEvctionClassSegment(bdlx)){
                String dtkssj = Utils.getkssjTime(scheduleMap,analysisDate);
                String dtjssj = Utils.getjssjTime(scheduleMap,analysisDate);
                List<Map<String,Object>> missCardlist = Utils.getAbnormalListBySchedule(scheduleMap,needRecordMissCardList,analysisDate);
                List<Map<String,Object>> abnormal = Utils.getAbnormalListBySchedule(scheduleMap,abnormalList,analysisDate);
                if (missCardlist.size() > 0){
                    String edfzs = Util.null2String(scheduleMap.get("edfzs")).equals("")?String.valueOf(Double.valueOf(scheduleMap.get("edxss").toString())*60):Util.null2String(scheduleMap.get("edfzs"));
                    lateParam.put("time",edfzs);
                    lateParam.put("workfor",Utils.getWorkFor(bdlx));
                    List<Map<String,Object>>  items = (List<Map<String,Object>>)commandExecutor.execute(new AbsenteeismItemCmd(lateParam)).get("attendanceItems");
                    if (items.size() > 0){
                        //去除异常
                        abnormalList = abnormalList.stream().filter(e->{
                            for (Map<String,Object> map:abnormal){
                                if (map.get("bdlx").equals(e.get("bdlx")) && map.get("pointTime").equals(e.get("pointTime"))){
                                    return false;
                                }
                            }
                            return true;
                        }).collect(Collectors.toList());
                        Map<String,Object> forgotMap = Maps.newHashMap();
                        forgotMap.putAll(missCardlist.get(0));
                        forgotMap.put("item",items.get(0).get("key"));
                        String hsdw = Util.null2String(items.get(0).get("hsdw"));
                        if (hsdw.equals(AccountingUnitEnum.HOUR.getKey())){
                            forgotMap.put("itemduration",scheduleMap.get("edxss"));
                        }else if (hsdw.equals(AccountingUnitEnum.DAY.getKey())){
                            forgotMap.put("itemduration",scheduleMap.get("edts"));
                        }else if (hsdw.equals(AccountingUnitEnum.MINUTES.getKey())){
                            forgotMap.put("itemduration",edfzs);
                        }else if (hsdw.equals(AccountingUnitEnum.ONCE.getKey())){
                            forgotMap.put("itemduration",1);
                        }
                        for (Map<String,Object> missCard: missCardlist){
                            if ((boolean)missCard.get("record")){
                                forgotMap.put("record",missCard.get("record"));
                                forgotMap.put("pointTime",missCard.get("pointTime"));
                                forgotMap.put("pointType",missCard.get("pointType"));
                                forgotMap.put("bdlx",missCard.get("bdlx"));
                            }
                        }
                        forgotMap.put("bdlx",bdlx);
                        forgotMap.put("hsdw",hsdw);
                        forgotMap.put("kczgsc",items.get(0).get("kczgsc"));
                        forgotMap.put("betweenMinutes",edfzs);
                        forgotMap.put("hsl",items.get(0).get("hsl"));
                        forgotMap.put("classStartTime",dtkssj);
                        forgotMap.put("classEndTime",dtjssj);
                        //合并异常
                        absenteeismList.add(forgotMap);
                    }
                }
            }
        }
        if (absenteeismList.size() > 0){
            abnormalList.addAll(absenteeismList);
        }

        return abnormalList;
    }
}
